package cn.clwxy.gdz.controller;

import java.io.IOException;

import javax.persistence.criteria.CriteriaBuilder.In;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


@WebServlet(urlPatterns={"/smsServlet"})
public class SmsServlet extends HttpServlet {
	private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
	public static int mobile_code;
	
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String op = req.getParameter("op");
		resp.setContentType("application/json;charset=utf-8");
//		CustomerDao dao = new CustomerMqlImpl();
		HttpSession session = req.getSession();
//		Customer cus = (Customer) session.getAttribute("cus");
		if ("send".equals(op)) {
			HttpClient client = new HttpClient();
			PostMethod method = new PostMethod(Url);

			client.getParams().setContentCharset("GBK");
			method.setRequestHeader("ContentType",
					"application/x-www-form-urlencoded;charset=GBK");

			mobile_code = (int) ((Math.random() * 9 + 1) * 100000);
			String content = new String("您的验证码是：" + mobile_code
					+ "。请不要把验证码泄露给其他人。");
			try {
				Thread.sleep(120);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			String tel = req.getParameter("tel");
			if (tel == null || "".equals(tel)) {
				resp.sendRedirect(req.getContextPath() + "/page/zsd/main.jsp");
			}
			NameValuePair[] data = {// 提交短信
			new NameValuePair("account", "C29269964"), // 查看用户名是登录用户中心->验证码短信->产品总览->APIID
					new NameValuePair("password",
							"a2e73acae3550b05ad408885b0bdaf15"), // 查看密码请登录用户中心->验证码短信->产品总览->APIKEY
					// new NameValuePair("password",
					// util.StringUtil.MD5Encode("密码")),
					new NameValuePair("mobile", tel),
					new NameValuePair("content", content), };
			method.setRequestBody(data);

			try {
				client.executeMethod(method);

				String SubmitResult = method.getResponseBodyAsString();

				// System.out.println(SubmitResult);

				Document doc = DocumentHelper.parseText(SubmitResult);
				Element root = doc.getRootElement();
				String code = root.elementText("code");
				String msg = root.elementText("msg");
				String smsid = root.elementText("smsid");
				System.out.println(mobile_code);
				System.out.println(code);
				System.out.println(msg);
				System.out.println(smsid);

				if ("2".equals(code)) {
					System.out.println("短信提交成功");
				}

				resp.getWriter().print(mobile_code);

			} catch (HttpException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if ("verify".equals(op)) {
			int code = Integer.parseInt(req.getParameter("code"));
			System.out.println(code + "/" + mobile_code);
			if (code == mobile_code) {
				System.out.println("验证成功+" + code);
				resp.getWriter().print(true);
			} else {
				resp.getWriter().print(false);
			}
		} else if ("telChange".equals(op)) {
//			int code = Integer.parseInt(req.getParameter("code"));
//			System.out.println(code + "/" + mobile_code);
//			if (code == mobile_code) {
//				System.out.println("验证成功+" + code);
//				String oldTel = req.getParameter("oldTel");
//				String newTel = req.getParameter("newTel");
//				int result = dao.changeTel(oldTel, newTel);
//				if (result >= 1) {
//					cus.setTel(newTel);
//					resp.getWriter().print(true);
//				}
//			} else {
//				resp.getWriter().print(false);
//			}
		} else if("pwdChange".equals(op)){
//			int code = Integer.parseInt(req.getParameter("code"));
//			System.out.println(code + "/" + mobile_code);
//			if (code == mobile_code) {
//				System.out.println("验证成功+" + code);
//			int uid = cus.getUid();
//			String newPwd = req.getParameter("newPwd");
//			String tel = cus.getTel();
//			String pwdmd5 = MD5Encrypter.encrypt(newPwd+tel);
//			int result = dao.changePwd(uid, pwdmd5);
//			if (result >= 1) {
//				cus.setPwd(pwdmd5);
//				resp.getWriter().print(true);
//			}
//		} else {
//			resp.getWriter().print(false);
//		}
		}else if("emailChange".equals(op)){
//			int code = Integer.parseInt(req.getParameter("code"));
//			System.out.println(code + "/" + mobile_code);
//			if (code == mobile_code) {
//				System.out.println("验证成功+" + code);
//			int uid = cus.getUid();
//			String newEmail = req.getParameter("newEmail");
//			int result = dao.changePwd(uid, newEmail);
//			if (result >= 1) {
//				cus.setEmail(newEmail);
//				resp.getWriter().print(true);
//			}
//		} else {
//			resp.getWriter().print(false);
//		}
		}
	}

}

